using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace PortPing
{
    public partial class Form1 : Form
    {
        TcpClient m_client = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                btnConnect.Enabled = false;
                if (null == m_client)
                {
                    m_client = new TcpClient();
                    m_client.Connect(cmbIpAddress.Text, Int32.Parse(txtPort.Text));
                    btnConnect.Text = "Disconnect";
                    txtPort.Enabled = false;
                    cmbIpAddress.Enabled = false;
                    timer1.Start();
                }
                else
                {
                    m_client.Close();
                    m_client = null;
                    btnConnect.Text = "Connect";
                    txtPort.Enabled = true;
                    cmbIpAddress.Enabled = true;
                    lblAvailable.Text = String.Empty;
                    timer1.Stop();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                this.Cursor = Cursors.Default;
                btnConnect.Enabled = true;
                btnSend.Enabled = (String.Empty != txtContent.Text) && (null != m_client);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            IPAddress[] address = Dns.GetHostAddresses(Dns.GetHostName());
            cmbIpAddress.Items.AddRange(address);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (null != m_client)
            {
                lblAvailable.Text = m_client.Available.ToString() + " Bytes Available";
            }
            else
            {
                lblAvailable.Text = "";
            }

            if((null != m_client) && (!m_client.Connected))
            {
                btnConnect.Enabled = true;
                btnConnect.Text = "Connect";
                btnSend.Enabled = false;
                txtPort.Enabled = true;
                cmbIpAddress.Enabled = true;
            }
        }

        private void txtContent_TextChanged(object sender, EventArgs e)
        {
            btnSend.Enabled = (String.Empty != txtContent.Text) && (null != m_client);
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                txtContent.Enabled = false;
                btnSend.Enabled = false;
                this.Cursor = Cursors.WaitCursor;
                NetworkStream nstream = m_client.GetStream();
                Byte[] data = Encoding.UTF8.GetBytes(txtContent.Text);
                if (chkSendLength.Checked)
                {
                    Byte[] lbuffer = BitConverter.GetBytes(data.Length);
                    nstream.Write(lbuffer, 0, lbuffer.Length);
                }
                nstream.Write(data, 0, data.Length);
                txtContent.Text = String.Empty;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                this.Cursor = Cursors.Default;
                txtContent.Enabled = true;
            }
            txtContent.Update();
            Application.DoEvents();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            try
            {
                NetworkStream nstream = m_client.GetStream();
                btnRead.Enabled = false;
                this.Cursor = Cursors.WaitCursor;
                Int32 contentLength = m_client.Available;
                if(chkReadLength.Checked)
                {
                    Byte[] lbuffer = new Byte[4];
                    nstream.Read(lbuffer, 0, 4);
                    contentLength = BitConverter.ToInt32(lbuffer, 0);
                }
                Byte[] buffer = new Byte[contentLength];
                nstream.Read(buffer, 0, buffer.Length);
                String data = Encoding.UTF8.GetString(buffer);
                txtRead.Text = data;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                btnRead.Enabled = true;
                this.Cursor = Cursors.Default;
            }
            txtRead.Update();
            Application.DoEvents();
        }
    }
}